package com.chensheng.controller;

import com.chensheng.converter.OrderDTO2OrderMaster;
import com.chensheng.dto.OrderDTO;
import com.chensheng.enums.ResultErrorEnum;
import com.chensheng.exception.SellException;
import com.chensheng.model.OrderMaster;
import com.chensheng.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Author:ChenSheng
 * @Description:
 * @Date: Created in 下午 22:43 2018/5/17 0017
 */
@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map){
        Integer start = (page - 1) * size;
        Integer offset = size;
        List<OrderDTO> orderDTOList = orderService.findList(start, offset);
        Integer totalCount = orderService.count();
        totalCount = (totalCount / size) + 1;

        map.put("orderDTOList", orderDTOList);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalCount", totalCount);

        //orderDTOList.forEach(e->System.out.println(e.getOrderStatusEnum().getMsg()));

        return new ModelAndView("order/list", map);
    }

    @GetMapping("detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String, Object> map){

        OrderDTO orderDTO = new OrderDTO();
        try{
            orderDTO = orderService.findOne(orderId);
        }catch(Exception e){
            log.error("【卖家端查询订单详情】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/seller/order/list");
            return new ModelAndView("common/error", map);
        }

        map.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail", map);
    }

    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               Map<String, Object> map){
        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            OrderMaster orderMaster = OrderDTO2OrderMaster.convert(orderDTO);
            orderService.cancel(orderMaster);
        }catch(Exception e){
            log.error("【卖家端取消订单】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/seller/order/list");
            return new ModelAndView("common/error", map);
        }

        map.put("msg", ResultErrorEnum.ORDER_CANCEL_SUCCESS.getMsg());
        map.put("url", "/seller/order/list");

        return new ModelAndView("common/success", map);
    }

    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId,
                               Map<String, Object> map){
        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            OrderMaster orderMaster = OrderDTO2OrderMaster.convert(orderDTO);
            orderService.finish(orderMaster);
        }catch(Exception e){
            log.error("【卖家端完结订单】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/seller/order/list");
            return new ModelAndView("common/error", map);
        }

        map.put("msg", ResultErrorEnum.ORDER_FINISH_SUCCESS.getMsg());
        map.put("url", "/seller/order/list");

        return new ModelAndView("common/success", map);
    }

}
